package cn.edu.lingnan.controllerV1;

import cn.edu.lingnan.service.PaperExportService;
import cn.edu.lingnan.service.PaperService;
import cn.edu.lingnan.user.dtos.PromptDto;
import cn.edu.lingnan.user.dtos.Result;
import cn.edu.lingnan.user.vos.PaperWithQuestionVo;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/paper")
@Slf4j
public class PaperExportController {
    
    @Autowired
    private PaperExportService exportService;



    @GetMapping("/export/{paperId}")
    public void exportPaper(HttpServletResponse response, @PathVariable Integer paperId) {
         log.info("导出试卷,{}",paperId);
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.setHeader("Content-Disposition", "attachment; filename=paper_" + paperId + ".docx");
          exportService.exportPaper(paperId, response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException("Error exporting paper", e);
        }
    }


    /**
     * 生成试卷,响应的是一个html文件
     */
    @PostMapping(value = "/generate_paper", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public void generatePaper(
            @RequestPart("generatePaperDto") String generatePaperDtoStr, // JSON 作为字符串
            @RequestPart(value = "word", required = false) MultipartFile word,
            @RequestPart(value = "image", required = false) MultipartFile image,
            HttpServletResponse response
    ) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setCharacterEncoding("UTF-8");

        // 使用 HuTool 的 JSONUtil 解析 JSON
        JSONObject jsonObject = JSONUtil.parseObj(generatePaperDtoStr);

        // 获取 "paperName" 字段的值
        String paperName = jsonObject.getStr("paperName");

        // 设置文件名到 Content-Disposition 头
        response.setHeader("Content-Disposition", "attachment; filename=" + paperName + ".docx");

        // 将 JSON 字符串反序列化为 DTO 对象
        ObjectMapper objectMapper = new ObjectMapper();
        PromptDto generatePaperDto = objectMapper.readValue(generatePaperDtoStr, PromptDto.class);

        // 调用服务层生成试卷
        exportService.generatePaper(generatePaperDto, response.getOutputStream(), word, image);
    }


}